<template>
  <div class="login-container">
    <div class="login-content">
      <div class="login-left"></div>

      <div class="login-right">
        <div class="login-title">
          <span>用户{{ registerStatus ? "注册" : "登录" }}</span>
        </div>

        <div class="login-form">
          <el-form ref="formRef" :model="form" :rules="rules">
            <el-form-item prop="username">
              <el-input
                placeholder="请输入用户名"
                maxLength="30"
                v-model="form.username"
                @keyup.enter="enterLogin"
              ></el-input>
            </el-form-item>

            <el-form-item prop="password">
              <el-input
                placeholder="请输入密码"
                maxLength="30"
                v-model="form.password"
                type="password"
                @keyup.enter="enterLogin"
              ></el-input>
            </el-form-item>

            <el-form-item>
              <el-button
                v-if="registerStatus"
                :loading="loginLoading"
                style="width: 100%"
                color="#626aef"
                @click="handleRegister"
                >立即注册</el-button
              >
              <el-button
                v-else
                :loading="loginLoading"
                style="width: 100%"
                color="#1D2532"
                @click="login"
                >立即登录</el-button
              >
            </el-form-item>
          </el-form>
        </div>

        <div class="login-footer">
          <span v-if="registerStatus"
            >已有账号，<span
              @click="register(false)"
              style="color: #1d2532; font-weight: bold; cursor: pointer"
              >立即登录</span
            >?</span
          >
          <span v-else
            >还没账号，<span
              @click="register(true)"
              style="color: #1d2532; font-weight: bold; cursor: pointer"
              >立即注册</span
            >?</span
          >
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, ref } from "vue";
import { ElForm, ElMessage } from "element-plus";
import axios from "../../util/request";

import { useRouter } from "vue-router";

const $router = useRouter();

const loginLoading = ref(false);
const formRef = ref(null);
const form = ref({
  username: "",
  password: "",
});
const registerStatus = ref(false);
const rules = ref({
  username: [
    {
      required: true,
      message: "请输入用户名",
      trigger: "blur",
    },
  ],
  password: [
    {
      required: true,
      message: "请输入密码",
      trigger: "blur",
    },
  ],
});

onMounted(() => {});

// 
const enterLogin = () => {
  if (registerStatus.value) {
    handleRegister();
  } else {
    login();
  }
};

// 注册状态
const register = (value) => {
  registerStatus.value = value;
}

// 登录
const login = () => {
  loginLoading.value = true;
  formRef["value"].validate(async (valid) => {
    if (valid) {
      const result = await axios.post(
        "/login",
        {
          user_name: form.value.username,
          password: form.value.password,
        }
      )
      
      const { code } = result.data;
      if (code === 200) {
        ElMessage.success("登录成功");
        $router.push("/");
      }
    } else {
      loginLoading.value = false;
    }
  });
};

// 注册
const handleRegister = () => {
  loginLoading.value = true;
  formRef.value.validate(async (valid) => {
    if (valid) {
      const result = await axios.post(
        "/register",
        {
          user_name: form.value.username,
          password: form.value.password,
        }
      )

      const { code } = result.data;
      if (code === 200) {
        ElMessage.success("注册成功,请登录");
      } else {
        ElMessage.error("注册失败");
      }
      loginLoading.value = false;
    } else {
      loginLoading.value = false;
    }
  });
};
</script>

<style lang="less">
.login-container {
  height: 100vh;
  width: 100%;
  overflow: hidden;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  background-image: url("../../assets/images/loginBg.jpg");
  background-repeat: no-repeat;
  background-size: 100% 100%;
  .login-content {
    display: flex;
    width: 30%;
    height: 40%;
    border-radius: 20px;
    overflow: hidden;
    margin-right: 5%;
    box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2);
    .login-left {
      width: 35%;
      height: 100%;
      background-color: rgb(29, 37, 50, 0.2);
    }
    .login-right {
      display: flex;
      align-items: center;
      justify-content: center;
      flex-direction: column;
      width: 65%;
      height: 100%;
      background-color: rgba(255, 255, 255, 1);
      .login-title {
        font-size: 18px;
        font-weight: 800;
        padding: 20px;
        color: #1d2532;
      }
      .login-form {
        width: 70%;
        margin: 0 auto;
      }
      .login-form ::v-deep(.el-input__inner) {
        border: none;
        outline: none;
      }

      .login-footer {
        font-size: 12px;
        color: #8f959e;
      }
    }
  }
}
</style>